<template>
  <div class="todo-item" :class="{ 'todo-completed': todo.status === 1 }">
    <div class="todo-content">
      <div class="todo-header">
        <input 
          type="checkbox" 
          :checked="todo.status === 1" 
          @change="toggleStatus"
          class="todo-checkbox"
        />
        <h3 class="todo-title">{{ todo.title }}</h3>
        <div class="priority-badge" :class="priorityClass">
          {{ priorityText }}
        </div>
      </div>
      <p class="todo-description">{{ todo.content }}</p>
      <div class="todo-date">
        <small>创建时间: {{ formatDate(todo.createTime) }}</small>
      </div>
    </div>
    <div class="todo-actions">
      <button @click="editTodo" class="btn btn-edit">编辑</button>
      <button @click="deleteTodo" class="btn btn-delete">删除</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TodoItem',
  props: {
    todo: {
      type: Object,
      required: true
    }
  },
  computed: {
    priorityClass() {
      switch(this.todo.priority) {
        case 1: return 'priority-low';
        case 2: return 'priority-medium';
        case 3: return 'priority-high';
        default: return 'priority-low';
      }
    },
    priorityText() {
      switch(this.todo.priority) {
        case 1: return '低';
        case 2: return '中';
        case 3: return '高';
        default: return '低';
      }
    }
  },
  methods: {
    formatDate(dateString) {
      if (!dateString) return '';
      const date = new Date(dateString);
      return date.toLocaleString();
    },
    toggleStatus() {
      const newStatus = this.todo.status === 1 ? 0 : 1;
      this.$emit('update-status', this.todo.id, newStatus);
    },
    editTodo() {
      this.$emit('edit', this.todo);
    },
    deleteTodo() {
      this.$emit('delete', this.todo.id);
    }
  }
}
</script>

<style scoped>
.todo-item {
  display: flex;
  justify-content: space-between;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  transition: all 0.3s ease;
}

.todo-completed {
  opacity: 0.7;
  background-color: #f8f8f8;
}

.todo-completed .todo-title {
  text-decoration: line-through;
  color: #888;
}

.todo-content {
  flex: 1;
}

.todo-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.todo-checkbox {
  margin-right: 10px;
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.todo-title {
  margin: 0;
  font-size: 18px;
  flex: 1;
}

.todo-description {
  margin: 8px 0;
  color: #555;
}

.todo-date {
  color: #888;
  font-size: 0.8rem;
}

.todo-actions {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
}

.btn {
  border: none;
  border-radius: 4px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background-color 0.2s;
}

.btn-edit {
  background-color: #4285f4;
  color: white;
}

.btn-edit:hover {
  background-color: #2a75f3;
}

.btn-delete {
  background-color: #ea4335;
  color: white;
}

.btn-delete:hover {
  background-color: #d73125;
}

.priority-badge {
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 0.8rem;
  color: white;
  margin-left: 10px;
}

.priority-low {
  background-color: #34a853;
}

.priority-medium {
  background-color: #fbbc05;
}

.priority-high {
  background-color: #ea4335;
}
</style> 